package android.slc.sys.ui;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.slc.appbase.api.slc.callback.SlcDisposableObserver;
import android.slc.appbase.utils.AutoServiceUtils;
import android.slc.core.ui.fragment.preference.MvvmPreferenceFragment;
import android.slc.extras.system.entify.AppVersions;
import android.slc.extras.system.repository.remote.VersionServiceRepository;
import android.slc.extras.user.repository.local.UserLocalService;
import android.slc.or.SlcCallbackConfig;
import android.slc.sys.R;

import androidx.annotation.Nullable;
import androidx.preference.Preference;

import com.blankj.utilcode.util.AppUtils;
import com.blankj.utilcode.util.ToastUtils;

import io.reactivex.rxjava3.disposables.Disposable;

public class SettingsFragment extends MvvmPreferenceFragment implements Preference.OnPreferenceClickListener {
    private Disposable checkUpDateDisposable;

    @Override
    public Object setContentView() {
        return R.xml.setting_preference;
    }

    @Override
    public void onBindView(@Nullable Bundle savedInstanceState, @Nullable String rootKey) {
        Preference personalInformation = findPreference("personal_information");
        personalInformation.setOnPreferenceClickListener(this);
        Preference checkForUpdates = findPreference("check_for_updates");
        checkForUpdates.setOnPreferenceClickListener(this);
        Preference aboutWe = findPreference("about_we");
        aboutWe.setOnPreferenceClickListener(this);
    }


    @Override
    public boolean onPreferenceClick(Preference preference) {
        switch (preference.getKey()) {
            case "personal_information":
                userInfo();
                break;
            case "check_for_updates":
                checkForUpdates();
                break;
            case "about_we":
                showAboutWe();
                break;
        }
        return false;
    }

    /**
     * 用户信息
     */
    public void userInfo() {
        startActivity(AutoServiceUtils.load(UserLocalService.class).getUserInfoIntent(requireContext()));
    }

    /**
     * 检查更新
     */
    @SuppressLint("AutoDispose")
    public void checkForUpdates() {
        checkUpDateDisposable = VersionServiceRepository.getVersionByLast()
                .subscribeWith(new SlcDisposableObserver<AppVersions>(SlcCallbackConfig.newBuilder()
                        .setDialogMsg(R.string.sys_label_check_for_updates_ing)
                        .setToastRes(R.string.sys_label_check_for_updates_error).build()) {
                    @Override
                    public void onSucceed(AppVersions data) {
                        if (data == null || data.getVersionCode() <= AppUtils.getAppVersionCode()) {
                            ToastUtils.showShort(R.string.title_already_the_latest_version);
                        } else {
                            UpDataActivity.showUpDataActivity(getContext(), data);
                        }
                    }

                    @Override
                    public void onFailed(int errorCode, String errorMessage) {

                    }
                });
    }

    private void showAboutWe() {
        AboutWeActivity.show(getContext());
    }

    @Override
    public void onDestroy() {
        if (checkUpDateDisposable != null && checkUpDateDisposable.isDisposed()) {
            checkUpDateDisposable.dispose();
        }
        super.onDestroy();
    }

}